package leetcode.队列和栈.栈;

import java.util.Stack;

public class Leetcode394 {
    public String decodeString(String s) {
        Stack<Integer> mutiRes = new Stack<>();
        Stack<String> string = new Stack<>();
        int muti = 0;
        String res ="";
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        for (int i = 0; i < n; i++) {

            char tmp = s.charAt(i);
            if(tmp=='['){//如果为'['就记录之前的数字和之前的字符串用来拼接
                mutiRes.push(muti);
                string.push(res);
                res = "";
                muti = 0;
            }else if(tmp==']'){//如果为']'就出栈之前的字符串和当前这个括号中的串有多少个
                int num = mutiRes.pop();
                String pre = string.pop();
                for (int i1 = 0; i1 < num; i1++) {
                    pre = pre+res;
                }
                res = pre;//返回给前面的使用
            }else if(tmp>'0'&&tmp<='9'){
                muti = muti*10+Integer.parseInt(tmp+"");
            }else{
                res = res + tmp + "";
            }
        }
        return res;
    }
}
